import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/house/house_list.dart';
import 'package:flutter_enjoy_plus/pages/house/location_list.dart';
import 'package:flutter_enjoy_plus/pages/login/index.dart';
import 'package:flutter_enjoy_plus/pages/notfound/index.dart';
import 'package:flutter_enjoy_plus/pages/notice/index.dart';
import 'package:flutter_enjoy_plus/pages/tab_bar_page.dart';
import 'package:flutter_enjoy_plus/profile/index.dart';
import 'package:flutter_enjoy_plus/utils/token.dart';

class RouterManager {
  // 路由组件的配置
  static final routes = {
      '/': (context) => const TabBarPage(),
      '/detail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
      // '/profile':(context) => ProfilePage()
    };

  // 路由拦截配置
  static generateRoute(RouteSettings settings){
          // 目标：没有token的拦截到登录页，有token才能访问
      final token = TokenManager().getToken() ?? '';
      // 是否有token。有token随便，无token跳转登录页
      if (token.isEmpty && settings.name != '/login') {
        // 若无token,且非登录页。则跳转登录页
        // if (settings.name != '/login') {
        return MaterialPageRoute(builder: (context) => const LoginPage());
        // }
      }
      // 如果有token，去想去的地方（手动设置）
      if (settings.name == '/profile') {
        return MaterialPageRoute(
            builder: (context) =>
                ProfilePage(userInfo: settings.arguments as Map));
      }

      if (settings.name == '/house_list') {
        return MaterialPageRoute(builder: (context) => const HouseList());
      }

      if (settings.name == '/location_list') {
        return MaterialPageRoute(builder: (context) => const LocationList());
      }
      // 有token，但与任何路径都不匹配
       return MaterialPageRoute(builder: (context) =>const NotFound());
  }
}